<?php

namespace Apps\AppApi\Method\Common;

use Apps\AppApi\Method\BaseMethod;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\OmBanner;

/**
 * 轮播图列表
 * @author
 * @date        2016年1月11日
 * @version     1.1.2
 * @copyright   Copyright 2015 meelier.com
 */
class Banner extends BaseMethod
{
    public function run()
    {
        $city_code = $this->getDataItem('city_code', '');
        $type = $this->getDataItem('type', '1'); // banner位id 1：首页 2：问答 3：美容院

        if (!in_array($type, ['1', '2', '3'])) {
            return $this->errorLog(ResultStatus::POST_BODY_FORMAT_ERROR, 'type类型错误');
        }

        if ($city_code) {
            $city_id = $this->getCityId($city_code) ?: '0';
            $where = "banner_name = {$type} and state = 1 and city_id in(0, {$city_id})";
        } else {
            $where = 'state = 1 and city_id = 0 and banner_name = ' . $type;
        }

        $list = OmBanner::query()->columns([
            'banner_id',
            'banner_data'
        ])->where($where)->orderBy('sort')->execute()->toArray();

        $qz = $this->getDi()->get('appConfig')->application->domainPic;
        $result = [];
        foreach ($list as $k => $val) {
            $val['banner_data'] = json_decode($val['banner_data'], true);
            $val['banner_data']['banner_id'] = $val['banner_id'];
            $val['banner_data']['link_data'] = json_decode($val['banner_data']['link_data'], true) ?: null;
            $val['banner_data']['url'] = $qz . $val['banner_data']['url'];

            $result[] = $val['banner_data'];
        }
        $this->setResult($result);
    }
}